//
//  JYTSetPasswardViewController.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 16/12/17.
//  Copyright © 2016年 nxa-yuanguoxiang. All rights reserved.
//

#import "JYTSetPasswardViewController.h"
#import "JYTLoginHandler.h"
#import "JYTLoginViewController.h"

@interface JYTSetPasswardViewController ()
@property (weak, nonatomic) IBOutlet UIButton *commitBtn;
@property (weak, nonatomic) IBOutlet UITextField *passField;
@property (weak, nonatomic) IBOutlet UITextField *checkPassField;

@end

@implementation JYTSetPasswardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSDictionary *dic = @{@"lftBtnBgImg":@"ic_goback",
                          @"lftBtnTitle":@"no",
                          @"pageTitle":@"新密码",
                          @"rhtBtnBgImg":@"no",
                          @"rhtBtnTitle":@"no"};
    
    [self setNavgationBarWithDic:dic];
    
    self.commitBtn.layer.cornerRadius = 5.0f;
    self.commitBtn.layer.masksToBounds = YES;
    [self.commitBtn setBackgroundColor:[UIColor colorWithRed:131/255.0f green:182/255.0f blue:248/255.0f alpha:1]];
    
}

- (IBAction)clickcommitBtn:(id)sender {
    
    NSString *passWord =  self.passField.text;
    if (!passWord || [passWord isEqualToString:@" "] || [passWord isEqualToString:@""])
    {
        [self showMessage:@"请输入密码"];
        return;
    }
    
    NSString *checkPassWord = self.checkPassField.text;
    if (!checkPassWord || [checkPassWord isEqualToString:@" "] || [checkPassWord isEqualToString:@""])
    {
        [self showMessage:@"请输入确认密码"];
        return;
    }
    
    if (![passWord isEqualToString:checkPassWord]) {
        [self showMessage:@"请检查输入是否正确"];
        return;
    }
    
    NSString *userName = self.userName;
    NSString *phone = [self.userName substringToIndex:10];
    MyLog(@"---------%@", phone);
//    if (userName.length > 11) {
//        phone = [self.userName substringToIndex:10];
//    } else {
//        phone = userName;
//    }
    
    [[JYTLoginHandler shared] changePwdWithUserName:userName andPhoneNum:phone andPwd:checkPassWord completionHandler:^(BOOL succ) {
        if (succ) {
            [self showMessage:@"设置成功"];
            [self gotoViewController:@"JYTLoginViewController"];
            
        } else {
            [self showMessage:@"设置失败"];
        }
        
    }];

}

- (void)showMessage:(NSString *)message
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    UIView *showview =  [[UIView alloc]init];
    showview.backgroundColor = [UIColor blackColor];
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.alpha = 1.0f;
    showview.layer.cornerRadius = 5.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc]init];
    label.text = message;
    
    //    CGSize LabelSize = [message sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(290, 9000)];
    //初始化段落，设置段落风格
    
    NSMutableParagraphStyle *paragraphstyle=[[NSMutableParagraphStyle alloc]init];
    paragraphstyle.lineBreakMode=NSLineBreakByCharWrapping;
    
    //设置label的字体和段落风格
    
    NSDictionary *dic=@{NSFontAttributeName:label.font,NSParagraphStyleAttributeName:paragraphstyle.copy};
    
    //计算label的真正大小,其中宽度和高度是由段落字数的多少来确定的，返回实际label的大小
    
    CGRect rect=[label.text boundingRectWithSize:CGSizeMake(290, 9000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    CGSize LabelSize = CGSizeMake(rect.size.width, rect.size.height);
    label.frame = CGRectMake(10, 5, LabelSize.width, LabelSize.height);
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:15];
    [showview addSubview:label];
    showview.frame = CGRectMake((CURRENT_SCREEN_WIDTH - LabelSize.width - 20)/2, CURRENT_SCREEN_HEIGHT - 100, LabelSize.width+20, LabelSize.height+10);
    [UIView animateWithDuration:2 animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
